﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Namespace UserAccountWrappers

    Public Class AccountMembershipService
        Implements IMembershipService
        Private provider As MembershipProvider

        Public Sub New()
            Me.New(Nothing)
        End Sub

        Public Sub New(ByVal provider As MembershipProvider)
            Me.provider = If(provider, Membership.Provider)
        End Sub

        Public ReadOnly Property MinPasswordLength() As Integer Implements IMembershipService.MinPasswordLength
            Get
                Return Me.provider.MinRequiredPasswordLength
            End Get
        End Property

        Public Function ValidateUser(ByVal userName As String, ByVal password As String) As Boolean Implements IMembershipService.ValidateUser
            Return Me.provider.ValidateUser(userName, password)
        End Function

        Public Function CreateUser(ByVal userName As String, ByVal password As String, ByVal email As String) As MembershipCreateStatus Implements IMembershipService.CreateUser
            Dim status As MembershipCreateStatus
            Me.provider.CreateUser(userName, password, email, Nothing, Nothing, True, Nothing, status)
            Return status
        End Function

        Public Function ChangePassword(ByVal userName As String, ByVal oldPassword As String, ByVal newPassword As String) As Boolean Implements IMembershipService.ChangePassword
            Dim currentUser = Me.provider.GetUser(userName, True) ' userIsOnline
            Return currentUser.ChangePassword(oldPassword, newPassword)
        End Function

        Public Function GetUserProfile(ByVal userName As String) As MembershipUser Implements IMembershipService.GetUserProfile
            Return Me.provider.GetUser(userName, False)
        End Function

        Public Function GetUser(ByVal userName As String) As MembershipUser Implements IMembershipService.GetUser
            Return Me.provider.GetUser(userName, False)
        End Function

        Public Function GetUserByProviderUserKey(ByVal providerUserKey As Object) As MembershipUser Implements IMembershipService.GetUserByProviderUserKey
            Return Me.provider.GetUser(providerUserKey, False)
        End Function

        Public Function CreateUser(ByVal userName As String, ByVal password As String, ByVal email As String, ByVal passwordQuestion As String, ByVal passwordAnswer As String, ByVal isApproved As Boolean, ByVal providerUserKey As Object, <System.Runtime.InteropServices.Out()> ByRef status As MembershipCreateStatus) As MembershipUser Implements IMembershipService.CreateUser
            Return Me.provider.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, status)
        End Function

        Public Function GetAllUsers() As MembershipUserCollection Implements IMembershipService.GetAllUsers
            Return Membership.GetAllUsers()
        End Function
    End Class
End Namespace